﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WSServer
{
    public class WSUtils
    {
        static WSUtils utils;

        public static WSUtils sharedInstance
        {
            get
            {
                if(utils == null)
                {
                    utils = new WSUtils();
                }
                return utils;
            }
        }

        public Task<WSMessageProcessResult> ProcessMessage(WSMessage message,Action<WSMessage> onRegisterService,Action<WSMessage> onRequestToken,Action<WSMessage> onReceiveMessage,Action<WSMessage> onReceiveMessageReceip)
        {
            return Task.Factory.StartNew<WSMessageProcessResult>(() =>
                {
                    WSMessageProcessResult result = new WSMessageProcessResult();
                    result.processed_message = message;
                    try
                    {
                        WSRequest request = message.Get<WSRequest>();
                        if (!request.isValidRequestWithSocket(message.socket))
                        {
                            result.isProcessedSuccess = false;
                            result.reason = "请求非法";
                            return result;
                        }
                        if(request.message_type == WSSocketReceiveMessageType.kSocketRecvMessageTypeRegisterService)
                        {
                            string identifier = request.identifier;
                            WSSocketCollection.collection.DeleteSocketWithIdentifier(identifier);
                            message.socket.identifier = identifier;
                            onRegisterService(message);
                        }
                        else if(request.message_type == WSSocketReceiveMessageType.kSocketRecvMessageTypeRefreshToken)
                        {
                            onRequestToken(message);
                        }
                        else if(request.message_type == WSSocketReceiveMessageType.kSocketRecvMessageTypeMessage)
                        {
                            onReceiveMessage(message);
                        }
                        else if(request.message_type == WSSocketReceiveMessageType.kSocketRecvMessageTypeRecvRecip)
                        {
                            onReceiveMessageReceip(message);
                        }
                        else
                        {
                            result.isProcessedSuccess = false;
                            result.reason = "未知错误";
                        }
                    }
                    catch(Exception e)
                    {
                        result.isProcessedSuccess = false;
                        result.reason = e.Message;
                    }
                    return result;
                });
        }
        public Task SendMessage(string identifier,string message)
        {
            WSSocket socket = WSSocketCollection.collection.Find(w =>
            {
                return w.identifier == identifier;
            });
            if (socket != null)
            {
                if(socket.socket_status == WSSocketStatus.kSocketStatusAuthorized)
                    return socket.Send<string>(message);
                else
                {

                }
            }
            return Task.Factory.StartNew(() =>
            {

            });
        }
        public Task SendMessageToWSSocketWithIdentifier(string identifier,WSResponse obj)
        {
            WSSocket socket = WSSocketCollection.collection.Find(w =>
                {
                    return w.identifier == identifier;
                });
            if(socket != null)
            {
                obj.token = socket.token;
                return socket.Send<WSResponse>(obj);
            }
            return Task.Factory.StartNew(()=>
                {
                   
                });
        }
    }
}
